package com.atguigu.spzx.manager.task;

import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Component
public class OrderStatisticsTask {

    @Autowired
    OrderInfoMapper orderInfoMapper;

    @Autowired
    OrderStatisticsMapper orderStatisticsMapper;

    @Scheduled(cron = "0 0 2 * * ?")  //凌晨两点统计前一天的订单总金额
    //@Scheduled(cron = "0/20 * * * * ?")  // 定义定时任务，使用@Scheduled注解指定调度时间表达式
    public void orderTotalAmountStatistics() {
        String datetime = DateUtil.offsetDay(new Date(), -1).toString(new SimpleDateFormat("yyyy-MM-dd"));
        OrderStatistics orderStatistics = orderInfoMapper.getOrderStatistics(datetime);
        if(orderStatistics!=null){
            orderStatisticsMapper.insert(orderStatistics);
        }
    }
}
